home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / js / calCalendarSearchService.js < prev    next >
Text File  |  2008-01-09  |  6KB  |  145 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Sun Microsystems code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Sun Microsystems, Inc.
  18.  * Portions created by the Initial Developer are Copyright (C) 2007
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Daniel Boelzle <daniel.boelzle@sun.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. function calCalendarSearchListener(numOperations, finalListener) {
  39.     this.mFinalListener = finalListener;
  40.     this.mNumOperations = numOperations;
  41.     this.mResults = [];
  42.  
  43.     var this_ = this;
  44.     function cancelFunc() { // operation group has been cancelled
  45.         this_.notifyResult(null);
  46.     }
  47.     this.opGroup = new calOperationGroup(cancelFunc);
  48. }
  49. calCalendarSearchListener.prototype = {
  50.     mFinalListener: null,
  51.     mNumOperations: 0,
  52.     opGroup: null,
  53.  
  54.     notifyResult: function calCalendarSearchListener_notifyResult(result) {
  55.         var listener = this.mFinalListener
  56.         if (listener) {
  57.             if (!this.opGroup.isPending) {
  58.                 this.mFinalListener = null;
  59.             }
  60.             listener.onResult(this.opGroup, result);
  61.         }
  62.     },
  63.  
  64.     // calIGenericOperationListener:
  65.     onResult: function calCalendarSearchListener_onResult(aOperation, aResult) {
  66.         if (this.mFinalListener) {
  67.             if (!aOperation || !aOperation.isPending) {
  68.                 --this.mNumOperations;
  69.                 if (this.mNumOperations == 0) {
  70.                     this.opGroup.notifyCompleted();
  71.                 }
  72.             }
  73.             if (aResult) {
  74.                 this.notifyResult(aResult);
  75.             }
  76.         }
  77.     }
  78. };
  79.  
  80. const calCalendarSearchService_ifaces = [ Components.interfaces.nsISupports,
  81.                                           Components.interfaces.calICalendarSearchProvider,
  82.                                           Components.interfaces.calICalendarSearchService,
  83.                                           Components.interfaces.nsIClassInfo ];
  84.  
  85. function calCalendarSearchService() {
  86.     this.wrappedJSObject = this;
  87.     this.mProviders = new calInterfaceBag(Components.interfaces.calICalendarSearchProvider);
  88. }
  89. calCalendarSearchService.prototype = {
  90.     mProviders: null,
  91.  
  92.     QueryInterface: function calCalendarSearchService_QueryInterface(aIID) {
  93.         ensureIID(calCalendarSearchService_ifaces, aIID);
  94.         return this;
  95.     },
  96.  
  97.     // nsIClassInfo:
  98.     getInterfaces: function calCalendarSearchService_getInterfaces(count) {
  99.         count.value = calCalendarSearchService_ifaces.length;
  100.         return calCalendarSearchService_ifaces;
  101.     },
  102.     getHelperForLanguage: function calCalendarSearchService_getHelperForLanguage(language) {
  103.         return null;
  104.     },
  105.     contractID: "@mozilla.org/calendar/calendarsearch-service;1",
  106.     classDescription: "Calendar Search Service",
  107.     classID: Components.ID("{F5F743CD-8997-428e-BC1B-644E73F61203}"),
  108.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  109.     flags: 0,
  110.  
  111.     // calICalendarSearchProvider:
  112.     searchForCalendars: function calCalendarSearchService_searchForCalendars(aString,
  113.                                                                              aHints,
  114.                                                                              aMaxResults,
  115.                                                                              aListener) {
  116.         var groupListener = new calCalendarSearchListener(this.mProviders.size, aListener);
  117.         function searchForCalendars_(provider) {
  118.             try {
  119.                 groupListener.opGroup.add(provider.searchForCalendars(aString,
  120.                                                                       aHints,
  121.                                                                       aMaxResults,
  122.                                                                       groupListener));
  123.             } catch (exc) {
  124.                 Components.utils.reportError(exc);
  125.                 groupListener.onResult(null, []); // dummy to adopt mNumOperations
  126.             }
  127.         }
  128.         this.mProviders.forEach(searchForCalendars_);
  129.         return groupListener.opGroup;
  130.     },
  131.  
  132.     // calICalendarSearchService:
  133.     getProviders: function calCalendarSearchService_getProviders(out_aCount) {
  134.         var ret = this.mProviders.interfaceArray;
  135.         out_aCount.value = ret.length;
  136.         return ret;
  137.     },
  138.     addProvider: function calCalendarSearchService_addProvider(aProvider) {
  139.         this.mProviders.add(aProvider);
  140.     },
  141.     removeProvider: function calCalendarSearchService_removeProvider(aProvider) {
  142.         this.mProviders.remove(aProvider);
  143.     }
  144. };
  145.